﻿





using System;
using System.ComponentModel.DataAnnotations;
using Tunynet.Common;
using Tunynet;
using Tunynet.Utilities;
using System.Web.Mvc;

namespace Spacebuilder.Common
{
    /// <summary>
    /// 用户表单呈现的UserProfile实体
    /// </summary>
    public class UserProfileEditModel
    {
        /// <summary>
        /// 
        /// </summary>
        public UserProfileEditModel() { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="userProfile"></param>
        /// <param name="user"></param>
        public UserProfileEditModel(UserProfile userProfile, User user)
        {
            if (userProfile != null)
            {
                Birthday = userProfile.Birthday;
                BirthdayType = userProfile.BirthdayType;
                Email = !string.IsNullOrEmpty(userProfile.Email) ? userProfile.Email : user.AccountEmail;
                Gender = userProfile.Gender;
                HomeAreaCode = userProfile.HomeAreaCode;
                Introduction = userProfile.Introduction;
                Mobile = userProfile.Mobile;
                Msn = userProfile.Msn;
                NowAreaCode = userProfile.NowAreaCode;
                QQ = userProfile.QQ;
                UserId = userProfile.UserId;
            }
            if (user != null)
            {
                TrueName = user.TrueName;
                NickName = user.NickName;
                AccountEmail = user.AccountEmail;
                UserName = user.UserName;
                IsEmailVerified = user.IsEmailVerified;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userProfile"></param>
        /// <param name="user"></param>
        /// <param name="userbasecase"></param>
        /// <param name="usereachrequset"></param>
        public UserProfileEditModel(UserProfile userProfile, User user, BaseCase userbasecase, EachRequest usereachrequset):this(userProfile,user)
        {
            if (userbasecase != null)
            {
                Zodiac = userbasecase.Zodiac;
                BlindDate = userbasecase.BlindDate;
               BloodType =userbasecase.BloodType;
                Culinary=userbasecase.Culinary;
               Drinking =userbasecase.Drinking;
               FamilyMembers =userbasecase.FamilyMembers;
               Figure =userbasecase.Figure;
               Height =userbasecase.Height;
               Housework =userbasecase.Housework;
               License =userbasecase.License;
               LikeMovie =userbasecase.LikeMovie;
               LiveParents =userbasecase.LiveParents;
                MaritalStatus=userbasecase.MaritalStatus;
               MasterLanguage =userbasecase.MasterLanguage;
              Nation  =userbasecase.Nation;
              PurchaseCar  =userbasecase.PurchaseCar;
               Salary =userbasecase.Salary;
              Smoking  =userbasecase.Smoking;
               SonInLaw =userbasecase.SonInLaw;
              TheHousing  =userbasecase.TheHousing;
               Weight =userbasecase.Weight;
               WhenMarried = userbasecase.WhenMarried;
            }
            if (usereachrequset != null)
            {
                OtherHousingSituation = usereachrequset.OtherHousingSituation;
                OtherCharacter = usereachrequset.OtherCharacter;
               OtherEducation =usereachrequset.OtherEducation;
                OtherHeight=usereachrequset.OtherHeight;
                OtherHousingSituation=usereachrequset.OtherHousingSituation;
                OtherMaritalStatus=usereachrequset.OtherMaritalStatus;
                OtherOccupation=usereachrequset.OtherOccupation;
                OtherPlaceResidence=usereachrequset.OtherPlaceResidence;
                OtherPlaceWork=usereachrequset.OtherPlaceWork;
            }
        }

        #region 属性

        #region 隐私设置

        /// <summary>
        /// Email隐私设置
        /// </summary>
        [Display(Name = "Email隐私设置")]
        public PrivacyStatus PrivacyEmail { get; set; }

        /// <summary>
        /// 手机号隐私设置
        /// </summary>
        [Display(Name = "手机号隐私设置")]
        public PrivacyStatus PrivacyMobile { get; set; }

        /// <summary>
        /// 生日隐私设置
        /// </summary>
        [Display(Name = "生日隐私设置")]
        public PrivacyStatus PrivacyBirthday { get; set; }

        /// <summary>
        /// QQ隐私设置
        /// </summary>
        [Display(Name = "QQ隐私设置")]
        public PrivacyStatus PrivacyQQ { get; set; }

        /// <summary>
        /// MSN隐私设置
        /// </summary>
        [Display(Name = "MSN隐私设置")]
        public PrivacyStatus PrivacyMSN { get; set; }

        #endregion

        /// <summary>
        /// 用户ID
        /// </summary>
        public long UserId { get; set; }

        /// <summary>
        /// 用户名
        /// </summary>
        [Display(Name = "用户名")]
        [Required(ErrorMessage = "请输入用户名")]
        [Remote("ValidateThirdUserName", "Account", ErrorMessage = "不合法的用户名")]
        [DataType(DataType.Text)]
        public string UserName { get; set; }

        /// <summary>
        ///帐号邮箱
        /// </summary>
        [Display(Name = "帐号邮箱")]
        [Required(ErrorMessage = "帐号邮箱为必填选项")]
        [RegularExpression(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$", ErrorMessage = "不合法的帐号邮箱")]
        public string AccountEmail { get; set; }

        /// <summary>
        ///姓名
        /// </summary>
        [Display(Name = "姓名")]
        [RegularExpression(@"^[\w|u4e00-\u9fa5]{1,64}$", ErrorMessage = "不允许使用的姓名")]
        [StringLength(30, ErrorMessage = "姓名最大长度允许30个字符")]
        [DataType(DataType.Text)]
        public string TrueName { get; set; }

        /// <summary>
        ///昵称
        /// </summary>
        [Display(Name = "昵称")]
        [Required(ErrorMessage = "请输入昵称")]
        [RegularExpression(@"^[\w|u4e00-\u9fa5]{1,64}$", ErrorMessage = "只能输入字母、数字、汉字和下划线")]
        //[Remote("ValidateNickNameForEditUserProfile", "Account", ErrorMessage = "不合法的昵称")]
        [DataType(DataType.Text)]
        public string NickName { get; set; }

        /// <summary>
        ///性别
        /// </summary>
        [Display(Name = "性别")]
        [Required(ErrorMessage = "必须选择一个性别")]
        public GenderType? Gender { get; set; }

        /// <summary>
        ///生日类型
        /// </summary>
        [Display(Name = "生日类型")]
        public BirthdayType BirthdayType { get; set; }

        /// <summary>
        ///公历生日
        /// </summary>
        [Display(Name = "生日")]
        public DateTime? Birthday { get; set; }

        /// <summary>
        ///农历年
        /// </summary>
        public int LunarYear { get; set; }

        /// <summary>
        ///农历月
        /// </summary>
        public int LunarMonth { get; set; }

        /// <summary>
        /// 农历日
        /// </summary>
        public int LunarDay { get; set; }

        /// <summary>
        ///所在地
        /// </summary>
        [Display(Name = "所在地")]
        public string NowAreaCode { get; set; }

        /// <summary>
        ///家乡
        /// </summary>
        [Display(Name = "家乡")]
        public string HomeAreaCode { get; set; }

        /// <summary>
        ///联系邮箱
        /// </summary>
        [Display(Name = "联系邮箱")]
        [RegularExpression(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$", ErrorMessage = "不合法的联系邮箱")]
        public string Email { get; set; }

        /// <summary>
        ///手机号码
        /// </summary>
        [Display(Name = "手机号码")]
        [RegularExpression("[0-9]{11}", ErrorMessage = "不合法的手机号码")]
        public string Mobile { get; set; }

        /// <summary>
        ///QQ
        /// </summary>
        [Display(Name = "QQ")]
        [StringLength(64, ErrorMessage = "QQ最大长度允许64个字符")]
        [RegularExpression("^[0-9]*[1-9][0-9]*$", ErrorMessage = "不合法的QQ号码")]
        public string QQ { get; set; }

        /// <summary>
        ///MSN
        /// </summary>
        [Display(Name = "MSN")]
        [StringLength(64, ErrorMessage = "MSN最大长度允许64个字符")]
        [RegularExpression(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$", ErrorMessage = "不合法的MSN帐号")]
        public string Msn { get; set; }

        /// <summary>
        ///Skype
        /// </summary>
        [Display(Name = "Skype")]
        public string Skype { get; set; }

        /// <summary>
        ///飞信
        /// </summary>
        [Display(Name = "飞信")]
        public string Fetion { get; set; }

        /// <summary>
        ///阿里旺旺
        /// </summary>
        [Display(Name = "阿里旺旺")]
        public string Aliwangwang { get; set; }

        /// <summary>
        ///证件类型
        /// </summary>
        [Display(Name = "证件类型")]
        public CertificateType CardType { get; set; }

        /// <summary>
        ///证件号码
        /// </summary>
        [Display(Name = "证件号码")]
        public string CardID { get; set; }

        /// <summary>
        ///自我介绍
        /// </summary>
        [Display(Name = "自我介绍")]
        [StringLength(200, ErrorMessage = "自我介绍最大长度允许200个字符")]
        [DataType(DataType.MultilineText)]
        public string Introduction { get; set; }


        #region 补充

             #region  个人情况

        /// <summary>
        /// 身高
        /// </summary>
        [Display(Name = "身高")]
        public string Height { get; set; }

        /// <summary>
        /// 体重
        /// </summary>
        [Display(Name = "体重")]
        public string Weight { get; set; }

        /// <summary>
        /// 婚姻状况（未婚，离异，丧偶，未婚有子女，离异有子女，丧偶有子女）
        /// </summary>
        [Display(Name = "婚姻状况")]
        public string MaritalStatus { get; set; }
        /// <summary>
        ///属相 即鼠(子)、牛(丑)、虎(寅)、兔(卯)、龙(辰)、蛇(巳)、马(午)、羊(未)、猴(申)、鸡(酉)、狗(戌)、猪(亥)
        /// </summary>
        [Display(Name = "属相")]
        public string Zodiac { get; set; }

        /// <summary>
        /// 家庭成员
        /// </summary>
        [Display(Name = "家庭成员")]
        public string FamilyMembers { get; set; }

        /// <summary>
        /// 是否有相亲时间（随时可以；节假日有空；年底有空；）
        /// </summary>
        [Display(Name = "是否有相亲时间")]
        public string BlindDate { get; set; }

        /// <summary>
        /// 购车情况（未购车，已购车，单位用车，需要时购置）
        /// </summary>
        [Display(Name = "购车情况")]
        public string PurchaseCar { get; set; }

        /// <summary>
        /// 是否有驾照(是；否；正在办理)
        /// </summary>
        [Display(Name = "驾照")]
        public string License { get; set; }

        /// <summary>
        /// 是否吸烟（不吸，很反感，不吸，但不反感，社交时偶尔吸， 烟不离手）
        /// </summary>
        [Display(Name = "是否吸烟")]
        public string Smoking { get; set; }

        /// <summary>
        /// 是否饮酒（不喝酒，社交需要喝，兴致时小酌，酒不离口）
        /// </summary>
        [Display(Name = "是否饮酒")]
        public string Drinking { get; set; }

        /// <summary>
        /// 月薪
        /// </summary>
        [Display(Name = "月薪")]
        public string Salary { get; set; }

        /// <summary>
        /// 是否愿意上门女婿（可以考虑）
        /// </summary>
        [Display(Name = "是否愿做上门女婿")]
        public string SonInLaw { get; set; }

        /// <summary>
        /// 住房情况（与父母同住，租房，已购房，住单位房，住亲朋家）
        /// </summary>
        [Display(Name = "住房情况")]
        public string TheHousing { get; set; }

        /// <summary>
        /// 民族（汉族，蒙古族，回族，藏族，维吾尔族，苗族，壮族，布依族，朝鲜族，满族，其他民族）
        /// </summary>
        [Display(Name = "民族")]
        public string Nation { get; set; }

        /// <summary>
        /// 血型（A型，B型，AB型，O型，其他）
        /// </summary>
        [Display(Name = "血型")]
        public string BloodType { get; set; }

        /// <summary>
        /// 体型（很瘦，较瘦，苗条，匀称，高挑，丰满，健壮，较胖，胖）
        /// </summary>
        [Display(Name = "体型")]
        public string Figure { get; set; }

        /// <summary>
        /// 掌握语言（中文（普通话），中文（广东话），英语，法语，日语，韩语，德语，意大利语，俄语）
        /// </summary>
        [Display(Name = "掌握语言")]
        public string MasterLanguage { get; set; }

        /// <summary>
        /// 家务（任劳任怨，希望对方承担家务，一起分工合作，看各自忙闲协商分担）
        /// </summary>
        [Display(Name = "家务")]
        public string Housework { get; set; }

        /// <summary>
        /// 厨艺（色香味俱全，能做几样可口小菜，不太会愿意为心爱的人学习）
        /// </summary>
        [Display(Name = "厨艺")]
        public string Culinary { get; set; }

        /// <summary>
        /// 何时结婚（及时闪婚，一年以内，两年以内，三年以内，时机成熟时结婚）
        /// </summary>
        [Display(Name = "何时结婚")]
        public string WhenMarried { get; set; }

        /// <summary>
        /// 是否愿意和父母同住（愿意，不愿意，视具体情况而定）
        /// </summary>
        [Display(Name = "是否愿意父母同住")]
        public string LiveParents { get; set; }

        /// <summary>
        /// 喜欢看的电影
        /// </summary>
        [Display(Name = "喜欢的电影")]
        public string LikeMovie { get; set; }
        #endregion

             #region 理想中对方情况

        /// <summary>
        /// 理想中对方学历
        /// </summary>
        [Display(Name = "学历")]
        public string OtherEducation { get; set; }

        /// <summary>
        /// 理想中对方身高
        /// </summary>
        [Display(Name = "身高")]
        public string OtherHeight { get; set; }

        /// <summary>
        /// 理想中对象性格
        /// </summary>
        [Display(Name = "性格")]
        public string OtherCharacter { get; set; }

        /// <summary>
        /// 理想中对象婚姻状况
        /// </summary>
        [Display(Name = "婚姻状况")]
        public string OtherMaritalStatus { get; set; }

        /// <summary>
        /// 理想中对象住房情况
        /// </summary>
        [Display(Name = "住房情况")]
        public string OtherHousingSituation { get; set; }

        /// <summary>
        /// 理想中对象职业
        /// </summary>
        [Display(Name = "职业")]
        public string OtherOccupation { get; set; }

        /// <summary>
        /// 理想中对象户口
        /// </summary>
        [Display(Name = "户口")]
        public string OtherRegistered { get; set; }

        /// <summary>
        /// 理想中对象居住地点
        /// </summary>
        [Display(Name = "居住地点")]
        public string OtherPlaceResidence { get; set; }

        /// <summary>
        /// 理想中对象工作地点
        /// </summary>
        [Display(Name = "工作地点")]
        public string OtherPlaceWork { get; set; }

        #endregion

        #endregion


        /// <summary>
        ///帐号邮箱是否通过验证
        /// </summary>
        public bool IsEmailVerified { get; set; }

        #endregion

        /// <summary>
        /// 转换为UserProfile用于数据库存储
        /// </summary>
        public UserProfile AsUserProfile(long userId)
        {
            UserProfile userProfile;
            if (UserId > 0)
            {
                UserProfileService userProfileService = new UserProfileService();
                userProfile = userProfileService.Get(UserId);
                userProfile.BirthdayType = this.BirthdayType;

                if (this.Gender == GenderType.FeMale)
                    userProfile.Gender = GenderType.FeMale;
                else if (this.Gender == GenderType.Male)
                    userProfile.Gender = GenderType.Male;

                else
                    userProfile.Gender = 0;

                if (this.BirthdayType == BirthdayType.LunarBirthday)
                {
                    ChinaDateTime cdt = new ChinaDateTime(LunarYear, LunarMonth, LunarDay);
                    userProfile.LunarBirthday = new DateTime(LunarYear, LunarMonth, LunarDay);
                    userProfile.Birthday = cdt.ToDateTime();

                }
                else
                {
                    if (Birthday != null)
                    {
                        ChinaDateTime cdt = new ChinaDateTime(Birthday.Value);
                        userProfile.Birthday = Birthday.Value;
                        userProfile.LunarBirthday = new DateTime(cdt.Year, cdt.Month, cdt.DayOfMonth);
                    }
                }

                if (!string.IsNullOrEmpty(Introduction))
                    userProfile.Introduction = this.Introduction;
                else
                    userProfile.Introduction = "";

                if (!string.IsNullOrEmpty(Email))
                    userProfile.Email = this.Email;
                else
                    userProfile.Email = "";

                if (!string.IsNullOrEmpty(Mobile))
                    userProfile.Mobile = this.Mobile;
                else
                    userProfile.Mobile = "";

                if (!string.IsNullOrEmpty(Msn))
                    userProfile.Msn = this.Msn;
                else
                    userProfile.Msn = "";

                if (!string.IsNullOrEmpty(NowAreaCode))
                    userProfile.NowAreaCode = this.NowAreaCode;
                else
                    userProfile.NowAreaCode = "";

                if (!string.IsNullOrEmpty(HomeAreaCode))
                    userProfile.HomeAreaCode = this.HomeAreaCode;
                else
                    userProfile.HomeAreaCode = "";

                if (!string.IsNullOrEmpty(QQ))
                    userProfile.QQ = this.QQ;
                else
                    userProfile.QQ = "";
            }
            else
            {
                userProfile = UserProfile.New();

                userProfile.BirthdayType = this.BirthdayType;

                if (this.Gender == GenderType.FeMale)
                    userProfile.Gender = GenderType.FeMale;
                else if (this.Gender == GenderType.Male)
                    userProfile.Gender = GenderType.FeMale;
                else
                    userProfile.Gender = 0;

                if (!string.IsNullOrEmpty(HomeAreaCode))
                    userProfile.HomeAreaCode = this.HomeAreaCode;
                else
                    userProfile.HomeAreaCode = "";

                if (!string.IsNullOrEmpty(NowAreaCode))
                    userProfile.NowAreaCode = this.NowAreaCode;
                else
                    userProfile.NowAreaCode = "";

                if (!string.IsNullOrEmpty(Email))
                    userProfile.Email = this.Email;
                else
                    userProfile.Email = "";

                if (!string.IsNullOrEmpty(Introduction))
                    userProfile.Introduction = this.Introduction;
                else
                    userProfile.Introduction = "";

                if (!string.IsNullOrEmpty(Mobile))
                    userProfile.Mobile = this.Mobile;
                else
                    userProfile.Mobile = "";

                if (!string.IsNullOrEmpty(Msn))
                    userProfile.Msn = this.Msn;
                else
                    userProfile.Msn = "";

                if (!string.IsNullOrEmpty(QQ))
                    userProfile.QQ = this.QQ;
                else
                    userProfile.QQ = "";

                if (Birthday != null)
                {
                    if (this.BirthdayType == BirthdayType.LunarBirthday)
                    {
                        ChinaDateTime cdt = new ChinaDateTime(LunarYear, LunarMonth, LunarDay);
                        userProfile.LunarBirthday = new DateTime(LunarYear, LunarMonth, LunarDay);
                        userProfile.Birthday = cdt.ToDateTime();

                    }
                    else
                    {
                        ChinaDateTime cdt = new ChinaDateTime(Birthday.Value);
                        userProfile.Birthday = Birthday.Value;
                        userProfile.LunarBirthday = new DateTime(cdt.Year, cdt.Month, cdt.DayOfMonth);
                    }
                }
                else
                {
                    ChinaDateTime cdt = new ChinaDateTime(DateTime.UtcNow);
                    userProfile.Birthday = DateTime.UtcNow;
                    userProfile.LunarBirthday = new DateTime(cdt.Year, cdt.Month, cdt.DayOfMonth);
                }

                userProfile.UserId = userId;

            }
            return userProfile;
        }

        /// <summary>
        /// 转换为User用于数据库存储
        /// </summary>
        public User AsUser(long userId)
        {
            IUserService userService = DIContainer.Resolve<IUserService>();
            User user = userService.GetUser(userId) as User;

            if (!string.IsNullOrEmpty(TrueName))
                user.TrueName = this.TrueName;
            else
                user.TrueName = "";

            if (!string.IsNullOrEmpty(NickName))
                user.NickName = this.NickName;
            else
                user.NickName = "";

            return user;
        }

    }

}